package com.ybwh.springboot2.common.fastjson.enumat;

import com.alibaba.fastjson.serializer.BeforeFilter;
import com.ybwh.springboot2.common.enums.CodeTextEnumUtils;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;


/**
 *
 * 注解新加字段
 *
 *
 */
@Slf4j
public class EnumAtBeforeFilter extends BeforeFilter {

    @Override
    public void writeBefore(Object object) {
        System.out.println("--------------" + object);


        Class<?> tmpClass = object.getClass();

        while (null != tmpClass) {
            Field[] fields = tmpClass.getDeclaredFields();
            for (Field f : fields) {
                f.setAccessible(true);
                EnumField enumAt = f.getAnnotation(EnumField.class);
                if (null != enumAt) {
                    try {
                        appendLabel(f.getName(), f.get(object), enumAt);
                    } catch (Exception e) {
                        log.error("", e);
                    }
                }
            }
            // 扫描父类
            tmpClass = tmpClass.getSuperclass();
        }
    }


    private void appendLabel(String name, Object value, EnumField enumAt) {
        System.out.println("-----------"+value);
        writeKeyValue(name + "_label", CodeTextEnumUtils.getDisplayName(enumAt.enumClass(),value));
    }



}
